<?php
/**
 * 订单管理
 */
namespace Admin\Controller;
use Org\Yike\Rights;
class OrderController extends CommonController {
    private $_ad_type = array('image'=>'图片', 'code'=>'代码', 'flash'=>'Flash', 'text'=>'文字');
    public $list_relation = false;
    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('Order');
    }

    public function _search() {
        $map = array();
        ($order_code = I('get.order_code')) && $map['order_code'] =$order_code;
        ($customer_name = I('get.customer_name')) && $map['customer_name'] =$customer_name;
        ($customer_phone = I('get.customer_phone')) && $map['customer_phone'] =$customer_phone;
        $this->assign('search', array(
            'order_code' => $order_code,
            'customer_name' => $customer_name,
            'customer_phone' => $customer_phone,
        ));
        return $map;
    }

    public function _before_index() {
        $big_menu = array(
            'title' => "添加商品",
            'iframe' =>U('Order/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '200',
        );
        $this->_ajaxedit = 1;

        $this->assign('big_menu', $big_menu);
    }

    public function _before_add() {
//        $adboard_types = $this->_adboard_mod->get_tpl_list();
//        $adboards = array();
//        foreach ($result as $val) {
//            $val['allow_type'] = implode('|', $adboard_types[$val['tpl']]['allow_type']);
//            $adboards[] = $val;
//        }
//        $board_id = I('get.board_id',0,'int');
//        $this->assign('site',getLinkMenu(1,'site_id'));
//        $this->assign('board_id',$board_id);
//        $this->assign('adboards', $adboards);
//        $this->assign('ad_type_arr', $this->_ad_type);
    }

    /**
     * 数组重组
     * @param $data
     * @return mixed
     */
    protected function _before_insert($data) {
        $data['order_code'] = create_code();
        $data['created_at'] = time();
        $data['created_username'] =  session('username');
        $data['updated_at'] =  time();
        $data['updated_username'] =  session('username');
        $data['status'] = 1;
        return $data;
    }

    public function detail() {
        $id = Y('id', 'intval');
        $detail = M('order')->where(['id'=>$id])->find();
        $this->assign('order',$detail);
        $this->display();
 }

    protected function _before_update($data) {
        //判断开始时间和结束时间是否合法
        /*
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] >= $data['end_time']) {
            $this->ajaxReturn(0, L('ad_endtime_less_startime'));
        }
        */
        switch ($data['type']) {
            case 'text':
                $data['content'] = I('post.text', 'trim');
                break;
            case 'image':
                $data['content'] =I('post.img', 'trim');
                break;
            case 'code':
                $data['content'] = I('post.code', 'trim');
                break;
            case 'flash':
                $data['content'] = I('post.flash', 'trim');
                break;
            default :
                $this->ajaxReturn(0, L('ad_type_error'));
                break;
        }
        $data['data'] = serialize($_POST['info']);

        return $data;
    }
    /**
     * ajax修改字段值
     */
    public function ajax_edit()
    {
        //AJAX修改数据
        $id = I('id');
        $field = I('get.field');
        $order = M('order');
        $order->$field = I('get.val');
        $order->updated_at = time();
        $order->updated_username = session('username');
        $order->where(array('id'=>$id))->save(); // 根据条件更新记录
        $this->ajaxReturn(1);
    }







    private function getAdboardSiteId($aid){
        $info = D("Adboard")->where(array('id'=>$aid))->getField('site_id');
        return $info;
    }

}